<?php
namespace app\lmsx\admin;

use app\common\builder\ZBuilder;
use think\Db;
use app\lmsx\model\User as UserModel;

/**
 * 用户
 */
class User extends Base
{
    public function index(){
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $btn_1=[
            'title' => '导出用户数据',
            'icon'  => 'fa fa-fw fa-download',
            'class' => 'btn btn-info',
            'href'  => url('export')
        ];
        $data = Db::table('dp_lmsx_user',true)
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $item['power']=explode(',',$item['power']);
                foreach ($item['power'] as $k=>&$v){
                    $arr=['无权限','查看资讯','查看文章'];
                    $v=$arr[$v];
                }
                $item['power']=implode("，",$item['power']);
                $arr_1=['暂无','学生','家长','其他'];
                $item['role']=$arr_1[$item['role']];
                // 对分页数据可以做额外处理
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('lmsx_user')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['name','姓名'],
                ['nickname','昵称'],
                ['avatar','头像','img_url'],
                ['phone','手机号码'],
                ['role','角色'],
                ['power','权限'],
                ['pull_black','是否拉黑用户','switch'],
                ['recommend_code','推荐识别码'],
                ['xcx_code','小程序码'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
//            ->addTopButton('add', [],[])
//            ->addRightButton('edit',[],[])
           // ->addRightButton('recode', ['icon' => 'fa fa-fw fa-hand-o-up', 'title' => '指定识别码','class' => 'btn btn-xs btn-default ajax-get', 'href' => url('recode', ['id' => '__id__'])],['area'=>['400px','50%']])
            ->addTopButtons(['delete'])
           // ->addTopButton('recode',$btn)
            ->addTopButton('export',$btn_1)
            ->setRowList($data)
            ->addFilter(['recommend_code','xcx_code'])
            ->setSearch(['name' => '姓名', 'nickname' => '昵称','phone'=>'手机号码'])
            ->fetch();
    }

    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'User.edit');
            if($result!==true) $this->error($result);
            //老师默认拥有查看资料库权限
            if(!empty($data['power'])){
                $data['power']=implode(",",$data['power']);
            }
            UserModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
//        $data = UserModel::get($id);
        $data=Db::table('dp_lmsx_user')->where('id',$id)->find();

        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],
                //['text', 'nickname', '昵称'],
                //['text', 'phone', '手机号码'],
//                ['radio','role','角色','',[1=>'学生',2=>'家长',3=>'其他']],
                ['checkbox','power','权限','',['1'=>'查看资料库']],
            ])
            ->setFormData($data)
            ->fetch();
    }


    //导出excel
    public function export()
    {
        // 查询数据
        $data = Db::table('dp_lmsx_user')->select();
//        halt($data);
        // 设置表头信息（对应字段名,宽度，显示表头名称）
        $cellName = [
            ['id', 'auto', 'ID'],
            ['name', 'auto', '姓名'],
            ['nickname', 'auto', '昵称'],
            ['role', 'auto', '角色'],
            ['phone', 'auto', '手机号码'],
            ['xcx_code', 'auto', '小程序分享码'],
            ['recommend_code', 'auto', '推荐识别码'],
            ['create_time', 'auto', '创建时间'],
        ];
        // 调用插件（传入插件名，[导出文件名、表头信息、具体数据]）
        plugin_action('Excel/Excel/export', ['用户数据', $cellName, $data]);
    }
}